/*
 *  Algorithm.cpp
 *  IncrementalAlgorithmSerial
 *
 *  Created by Roman Kutlak on 30/10/2010.
 *  Copyright 2010 Roman Kutlak. All rights reserved.
 *
 */

#include "Algorithm.h"
#include "Settings.h"

namespace kutlak {
namespace roman {
namespace gre {
    
    Algorithm::Algorithm(const pKnowledgeBase& kb, 
                         const pOntology& ontology, 
                         const string& name) :
    fPlural(false), fNegation(false), fDisjunction(false),
    fConjunction(false), fGradable(false), fDeterminer(true), mName(name),
    mpKnowledgeBase(kb), mpOntology(ontology)
    {
        if (!kb)
            mpKnowledgeBase = KnowledgeBase::Default();
        if (!ontology)
            mpOntology = Ontology::Default();
    }
    
    void Algorithm::BanAttribute(const Vindex_t& attribute)
    { mBannedAttributes.push_back(attribute); }
    
    void Algorithm::AllowAttribute(const Vindex_t& attribute)
    {
        for (auto it = mBannedAttributes.begin();
             it != mBannedAttributes.end(); ++it)
        {
            if (*it == attribute)
                it = mBannedAttributes.erase(it);
                
            
            if (it == mBannedAttributes.end())
                break;
        }
    }
    
    bool Algorithm::IsBannedAttribute(const Vindex_t& attribute)
    {
        for (auto it = mBannedAttributes.begin();
             it != mBannedAttributes.end(); ++it)
        {
            if (*it == attribute)
                return true;
        }
        
        return false;
    }
    
    void Algorithm::BanAttribute(const std::string& attribute)
    { BanAttribute(Voc::Instance().Index(attribute)); }
    
    void Algorithm::AllowAttribute(const std::string& attribute)
    { AllowAttribute(Voc::Instance().Index(attribute)); }
    
    bool Algorithm::IsBannedAttribute(const std::string& attribute)
    { return IsBannedAttribute(Voc::Instance().Index(attribute)); }
    
    
    RefExp Algorithm::MRE(const std::string& referentName)
    {
        RefExp result;
        if (!mpKnowledgeBase)
        { // kb not set
            if (KnowledgeBase::Default())
            {
                mpKnowledgeBase = KnowledgeBase::Default();
            }
            else
            {
                cerr << "GH::MRE: knowledge base not set.\n";
                return result;
            }
        }
        
        if (mpKnowledgeBase->Size() == 0)
        { // kb empty
            cerr << "GH::MRE: kb empty.\n";
            return result;
        }
        
        EntityPtrT referent = mpKnowledgeBase->EntityPtrByName(referentName);
        if (! referent)
        { // referent not found
            cerr << "GH::MRE: referent not found.\n";
            return result;
        }
        
        ContainerT distractors(mpKnowledgeBase->Entities());
        return this->MakeReferringExpression(referent, distractors);
    }
    
} // end namespace gre
}
}



